package com.fingard.dsp.bank.directbank.ceb02;

import com.fingard.dsp.bank.directbank.bankInterface.IInCustodyInfo;
import com.fingard.dsp.bank.reqrespobj.qryincustodyinfo.ReqQryInCustodyInfo;
import com.fingard.dsp.bank.reqrespobj.qryincustodyinfo.RetAtsInCustodyInfo;
import com.fingard.dsp.bank.reqrespobj.qryincustodyinfo.RetAtsInCustodyInfoDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 获取在押车辆信息（业务要求查询400:已入库 450:出库申请已提交 500:出库审核流程中）
 * @author F
 *
 */
public class CEB02QryInCustodyInfo extends CEB02Base implements IInCustodyInfo{

	@Override
	public RetAtsInCustodyInfo qryInCustodyInfo(ReqQryInCustodyInfo recInfo) throws Exception {
		RetAtsInCustodyInfo retAts = new RetAtsInCustodyInfo(recInfo.reqHeader);
		retAts.writeStart();
		String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
       // 	for(int i=0;i<3;i++) {
            int PAGE = 0;
            int BISHU = 30;
            for(int i=0;i<=PAGE;i++) {
            CEB02Header bankHeader = new CEB02Header();
            //银行获取相关信息
            bankHeader.FTranCode = "Q052";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader_3(xmlWriter, bankHeader); 
            //写入body
            xmlWriter.writeStartElement("Body");
            xmlWriter.writeElementString("CHANNEL_CODE", bankFront.org);//接入机构号
            xmlWriter.writeElementString("DEALER_CODE",recInfo.DealerCode);//接入经销商编码
    		//if(i==0) {
            xmlWriter.writeElementString("PI_STATUS","400" );//合格证状态 400:已入库 450:出库申请已提交500:出库审核流程中600:出库审核不通过 700:已出库760：出库审核通过800：超范围待确认 810：不监管
    		/*    }
    		if(i==1) {
                xmlWriter.writeElementString("PI_STATUS","450" );//合格证状态 400:已入库 450:出库申请已提交500:出库审核流程中600:出库审核不通过 700:已出库760：出库审核通过800：超范围待确认 810：不监管
        		}
    		if(i==2) {
                xmlWriter.writeElementString("PI_STATUS","500" );//合格证状态 400:已入库 450:出库申请已提交500:出库审核流程中600:出库审核不通过 700:已出库760：出库审核通过800：超范围待确认 810：不监管
        		}*/
            xmlWriter.writeElementString("QISHI", i*BISHU+"");//起始笔数
            xmlWriter.writeElementString("BISHU", BISHU+"");//查询笔数默认50
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(),bankHeader);//发送银行
            if(tmpStrRet[0].length() == 0){
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            	String tmpRetMsg = tmpBkReader.getFirstText("/Out/Body/ErrorInfo");
                String tmpRetCode = tmpBkReader.getFirstText("/Out/Head/ErrCode");
				    if (tmpRetCode.length()==0) {
				    	tmpRespCode = "0";
                    	tmpRespInfo = "获取成功";
				        //翻页处理
				        if(PAGE == 0) {
                            int Z_BISHU = Integer.valueOf(tmpBkReader.getFirstText("/Out/Body/Z_BISHU")); 
                            int count = Z_BISHU/BISHU;
                            if(count>0) {
                            	if(Z_BISHU%BISHU==0) {
                            		PAGE = count - 1;
                            	} else {
                            	PAGE = count;
                            	}
                            }
                        }
				        while (tmpBkReader.read("/Out/Body/Frame")) {
				        	RetAtsInCustodyInfoDetail tmpRetDetail = new RetAtsInCustodyInfoDetail();
				            tmpRetDetail.DealerCode = recInfo.DealerCode;
				            tmpRetDetail.VIN = tmpBkReader.readedXElement.getSingleText("DJ_NO");
				            tmpRetDetail.PickupPrice = tmpBkReader.readedXElement.getSingleText("CLJG");
				            tmpRetDetail.CarMortgageTime = "";
				            tmpRetDetail.CarEneDate = tmpBkReader.readedXElement.getSingleText("MATURE_DATE");
				            tmpRetDetail.BillNo = tmpBkReader.readedXElement.getSingleText("BF_NO");
				            tmpRetDetail.MainRepositoryLocation = tmpBkReader.readedXElement.getSingleText("CAR_STOCK_ADD");
				            tmpRetDetail.SecondRepositoryLocation = tmpBkReader.readedXElement.getSingleText("");
				            tmpRetDetail.SecondNetLocation = tmpBkReader.readedXElement.getSingleText("");
				            tmpRetDetail.Reserve1 = "";
				            tmpRetDetail.Reserve2 = "";
				            tmpRetDetail.Reserve3 = "";
				            tmpRetDetail.Reserve4 = "";
				            tmpRetDetail.Reserve5 = "";
				            retAts.writeRetDetail(tmpRetDetail);
				        }
				    } else {
				    	tmpRespCode = "-1";
				        tmpRespInfo = tmpRetMsg;
				        break;
				    }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
                break;
            }    
      //   }
         }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRespCode(tmpRespCode);
        retAts.writeRespInfo(tmpRespInfo);
        retAts.writeEnd();
        return retAts;
    }

}
